/**
 * 注册点击事件
 */
$("#register").click(function() {
	//1.校验数据
	if (!valodate_add_form()) {
		return false;
	}
	//2.后台校验
    if ($(this).attr("ajax_va") == "error") {
        return false;
    }
	var Vcode = $("#Vcode").val();
    //3.发送Ajax请求1
	$.ajax({
        url : "UserInfo?Vcode="+Vcode,
        type : "POST",
        data : $("#register_form form")
            .serialize(),
        success : function(result) {
            if (result.code == 100) {
               /* 清空表单
               $(':input','#register_form form')
					.removeAttr('ajax_va')
					.val('')
					.removeClass("has-error")
					.removeClass("has-success")*/
                alert("注册成功");
                window.location.href="layout";
            } else if(result.code == 200){
                FalseCss("#punstername",result.extend.va_msg);
        	}else {
            	alert("验证码错误");
			}
        }

	})
});
/**
 * url的父节点下的span的样子变化
 * 就是在验证栏下的span标签变化
 */
function FalseCss(url,msg){
	$(url).parent().removeClass("has-success");
	$(url).parent().addClass("has-error");
	$(url).next("span").text(msg);
}
function SuccessCss(url,msg){
	$(url).parent().removeClass("has-error");
	$(url).parent().addClass("has-success");
	$(url).next("span").text(msg);
}

/**
 * 前端校验数据
 * @returns {boolean}
 */
function valodate_add_form() {
	//1.拿到要校验的数据
	var punstername = $("#punstername").val();
	var regUserId = /(^[a-zA-Z0-9_-]{6,16}$)|([\u2E80-\u9FFF]{2,5})/;
	if (!regUserId.test(punstername)) {
        FalseCss("#punstername","用户名应为6-16位英文和数字的组合或者2-5位中文");
		return false;
	} else {
        SuccessCss("#punstername");
	}
	//2.校验密码
	var pwd = $("#password").val();
	var pwd2 = $("#pwd2").val();
	if (pwd == pwd2) {
        SuccessCss("#pwd2");
	} else {
        FalseCss("#pwd2","请输入相同的密码");
        return false;
	}
	return true;
}
//校验用户名是否可用(同名不可用)
$("#punstername").change(function() {
		var punstername = this.value;
		$.ajax({
			url : "checkuser",
			data : "punsterName=" + punstername,
			type : "POST",
			success : function(result) {
				if (result.code == 100) {
					SuccessCss("#punstername","用户名可用");
					$("#register").attr("ajax_va", "success");
				} else {
					FalseCss("#punstername",result.extend.va_msg)
					//result.extend.va_msg 后端返回的字段
					$("#register").attr("ajax_va", "error");
				}
			}
		});
});

$("#Vcode").change(function() {
    var Vcode = this.value;
    $.ajax({
        url : "checkCode",
        data : "Vcode=" + Vcode,
        type : "POST",
        success : function(result) {
            if (result.code == 100) {
                SuccessCss("#Vcode","验证码正确");
                $("#register").attr("ajax_va", "success");
            } else {
                FalseCss("#Vcode","验证码错误")
                //result.extend.va_msg 后端返回的字段
                $("#register").attr("ajax_va", "error");
            }
        }
    });
})
